package backstage.staff.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import backstage.staff.pojo.Staff;
import backstage.staff.service.StaffService;
import backstage.sys.action.PlatformAction;
import backstage.sys.util.Platform;
import backstage.util.tool.ToolUtil;

public class StaffAction extends PlatformAction{
	
	private static final long serialVersionUID = 1L;

	private String username;
	private String password;
	private String oldPassword;
	private String newPassword;
	private String error;
	private String groupId;

	public String execute() throws Exception {
		String req = "error";
		HttpSession session = this.getSession();
		if(null != this.username && null!= this.password){
			String request = null;
			try {
				StaffService staff = (StaffService)Platform.getInstance().getApplication().getBean("staffService");
				Staff po = new Staff();
				po.setSysName(this.username);
				po.setSysPwd(this.password);
				request = staff.verifi(po,session);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(null == request){
				req = "success";
			}else{
				this.error = "用户名不存在或密码不正确";
			}
		}else{
			this.error = "请输入用户名和密码";
		}
		return req;
	}

	public String addStaff() throws Exception {
		Staff po = (Staff)this.getSession().getAttribute("staff");
		if(null != po){
			if(null != po.getId() && !"".equals(po.getId())){
				int gid = Integer.parseInt(po.getCompetence().getGroupId());
				StaffService staff = (StaffService)Platform.getInstance().getApplication().getBean("staffService");
				Staff s = new Staff();
				return "not";
			}else{
				return "error";
			}
		}else{
			return "error";
		}
	}
	
	public String updateStaff() throws Exception {
		StaffService staff = (StaffService)Platform.getInstance().getApplication().getBean("staffService");
		Staff s = new Staff();
		staff.updateStaff(s);
		return "updatePwd";
	}
	
	public String updatePassword() throws Exception {
		StaffService staff = (StaffService)Platform.getInstance().getApplication().getBean("staffService");
		Staff po = (Staff)this.getSession().getAttribute("staff");
		if(!ToolUtil.isStringNull(this.oldPassword)){
			this.error = "密码不能为空!";
		}
		if(!ToolUtil.isStringNull(this.newPassword)){
			if(this.newPassword.equals(this.password)){
				po.setSysPwd(this.newPassword);
				try {
					staff.updateStaff(po);
				} catch (Exception e) {
					this.error = "更新错误!";
				}
			}else{
				this.error = "两次输入密码不正确!";
			}
		}else{
			this.error = "新输入密码不能为空!";
		}
		return "updatePwd";
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

}
